package com.eastjet.maintenance.contrller;

import com.eastjet.maintenance.config.Code;
import com.eastjet.maintenance.config.Result;
import com.eastjet.maintenance.domain.*;
import com.eastjet.maintenance.service.impl.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//到货验收/设备调拨
@RestController
@RequestMapping("/acceptance")
@CrossOrigin(origins = "*")
public class AcceptanceController {
    @Autowired
    private AcceptanceServiceImpl acceptanceService;//注入到货验收service
    @Autowired
    private AllocateServiceImpl allocateService;//注入设备调拨service
    @Autowired
    private EquipmentDisposalServiceImpl equipmentDisposalService;//设备处置service
    @Autowired
    private AllocationDetailsServiceImpl allocationDetailsServiceImpl;// 设备调拨明细

    @Autowired
    private DispositionDetailsServiceImpl dispositionDetailsServiceImpl;//设备处置详情Service

    /*
     * 保存到货验收信息
     */
    @PostMapping("/insert")
    public Result InsertAcceptance(@RequestBody Map<String, Object> map) {
        System.out.println("到货验收");
        Boolean b = acceptanceService.InsertAcceptance(map);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "保存成功" : "保存失败");
    }

    /*
     * 查询到货验收信息
     */
    @RequestMapping("/selectAcceptance")
    public Result SelectAllAcceptance(Acceptance acceptance, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Acceptance> list = acceptanceService.SelectByAcceptanceId(acceptance);
        PageInfo<Acceptance> pageInfo = new PageInfo<>(list);
        return new Result(pageInfo, list.isEmpty() ? Code.GET_ERR : Code.GET_OK, list.isEmpty() ? "查询为空" : "查询成功");
    }

    /*
    新增设备调拨数据
     */
    @PostMapping("/insertAllocate")
    public Result InsertAllocate(@RequestBody Map<String, Object> map) {
        Boolean b = allocateService.InsertAllocate(map);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "保存成功" : "保存失败");
    }

    /**
     * 查询设备调拨数据
     *
     * @return
     */
    @RequestMapping("/selectAllocate")
    public Result SelectAllocate(Allocate allocate, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Allocate> list = allocateService.SelectAllocate(allocate);
        PageInfo<Allocate> pageInfo = new PageInfo<>(list);
        return new Result(pageInfo, list.isEmpty() ? Code.GET_ERR : Code.GET_OK, list.isEmpty() ? "查询为空" : "查询成功");
    }

    /*
     * 查看设备调拨列表
     */
    @RequestMapping("/selectAllocateList")
    public Result SelectAllocateList(Allocate allocate) {
        Map<String, Object> map = new HashMap<>();
        List<Allocate> list = allocateService.SelectAllocate(allocate);
        List<AllocationDetails> allocationDetails = allocationDetailsServiceImpl.selectAllocationDetailsByAllocationId(allocate.getCode());
        map.put("list", list);
        map.put("allocationDetails", allocationDetails);
        return new Result(map, list.isEmpty() ? Code.GET_ERR : Code.GET_OK, list.isEmpty() ? "查询为空" : "查询成功");
    }


    @PostMapping("/insertEquipmentDisposal")
    public Result InsertEquipmentDisposal(@RequestBody Map<String, Object> map) {
        Boolean b = equipmentDisposalService.InsertEquipmentDisposal(map);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "保存成功" : "保存失败");
    }

    @RequestMapping("/selectEquipmentDisposal")
    public Result SelectEquipmentDisposal(EquipmentDisposal equipmentDisposal, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<EquipmentDisposal> list = equipmentDisposalService.SelectEquipmentDisposal(equipmentDisposal);
        PageInfo<EquipmentDisposal> pageInfo = new PageInfo<>(list);
        return new Result(pageInfo, list.isEmpty() ? Code.GET_ERR : Code.GET_OK, list.isEmpty() ? "查询为空" : "查询成功");
    }

    /*
     * 查看设备调拨列表
     */
    @RequestMapping("/SelectEquimentDisposalList")
    public Result SelectEquimentDisposalList(EquipmentDisposal equipmentDisposal) {
        System.out.println("查看设备处理" + equipmentDisposal.getCode());
        Map<String, Object> map = new HashMap<>();
        List<EquipmentDisposal> list = equipmentDisposalService.SelectEquipmentDisposal(equipmentDisposal);
        List<DispositionDetails> dispositionDetails = dispositionDetailsServiceImpl.selectDispositionDetailsByDispositionId(equipmentDisposal.getCode());
        map.put("list", list);
        map.put("dispositionDetails", dispositionDetails);
        return new Result(map, list.isEmpty() ? Code.GET_ERR : Code.GET_OK, list.isEmpty() ? "查询为空" : "查询成功");
    }

}
